﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WoltersKluwer.SPChainGang.Core.Util
{
    public static class LinkHelpers
    {
        public static Uri MakeUri(string baseUrl, string relativeUrl)
        {
            return new Uri(new Uri(baseUrl), relativeUrl);
        }

        public static string CombineLinks(string baseUrl, string relativeUrl)
        {
            return MakeUri(baseUrl,relativeUrl).ToString();
        }

        public static string StripInvalidCharacters(string input)
        {
            var illegalChars =
                new Regex(String.Format("[{0}]", Regex.Escape(new String(Path.GetInvalidFileNameChars()))));
            return illegalChars.Replace(input, "_");
        }

        public static string MakeRooted(string link)
        {
            if (!link.StartsWith("/"))
                link = link.Insert(0, "/");

            if (link.EndsWith("/"))
                link = link.Remove(link.Length - 1);

            return link;
        }
    }
}
